/* Define Canvas */

var canvas;
var stage;

/* [Graphics] */



/* Background */


var menuBg;

var phase1Bg;

var phase2Bg;

//DEBUG BUTTONS
var dbgB1;
var dbgB2;
var dbgB3;
var dbgCont = new Container();


/* Variables */

var TestBg = new Container();
var bullets1 = new Container();
var bullets2 = new Container();

var bossHealth = 20;
var score;
var gfxLoaded = 0;

var centerX = 160;
var centerY = 240;
var screenWidth = 320;
var screenHeight = 480;

var tkr = new Object();
var timerSource;


/* Main */

function Main()
{
	/* Link Canvas */
	
	canvas = document.getElementById('Shooter');
  	stage = new Stage(canvas);
  		
  	stage.mouseEventsEnabled = true;
  	
  	
  	
	//LoadGame();
	LoadMenu();
	/* Ticker */
	
	Ticker.setFPS(60);
	Ticker.addListener(stage);
	
	//Start Ticks
	Ticker.addListener(tkr, false);
	tkr.tick = update;
}



Bitmap.prototype.setWidth = function(w)
{       
    if (this.image.width == 0) return;
    
    this.scaleX = w / this.image.width;
}
    
Bitmap.prototype.setHeight = function(h)
{
    if (this.image.height == 0) return;

    this.scaleY = h / this.image.height;
}

/*
/* Sound 

	SoundJS.addBatch([
		{name:'boss', src:'boss.mp3', instances:1},
		{name:'explo', src:'explo.mp3', instances:10},
		{name:'shot', src:'shot.mp3', instances:10}]);

function alerteur(e)
{
	/* Remove Listeners 
		
	stage.onMouseMove = null;
	bg.onPress = null;
	bg2.onPress = null;
	
	Ticker.removeListener(tkr);
	tkr = null;
	
	timerSource = null;
	
	/* Display Correct Message 
	
	if(e == 'win')
	{
		win = new Bitmap(winImg);
		win.x = centerX - 64;
		win.y = centerY - 23;
		stage.addChild(win);
		stage.removeChild(enemies, boss);
	}
	else
	{
		lose = new Bitmap(loseImg);
		lose.x = centerX - 64;
		lose.y = centerY - 23;
		stage.addChild(lose);
		stage.removeChild(enemies, ship);
	}
	
	bg.onPress = function(){window.location.reload();};
	bg2.onPress = function(){window.location.reload();};
	stage.update();
}





<script src="easel.js"></script>
<script src="Tween.js"></script>
<script src="sound.js"></script>

*/
/*
function loadGfx(e)
{
	if(e.target.name = 'bg'){bg = new Bitmap(bgImg);}
	if(e.target.name = 'b1'){b1 = new Bitmap(b1Img);}
	if(e.target.name = 'b2'){b2 = new Bitmap(b2Img);}
	if(e.target.name = 'menuBg'){menuBg = new Bitmap(menuBgImg);}
	if(e.target.name = 'phase1Bg'){phase1Bg = new Bitmap(phase1BgImg);}
	if(e.target.name = 'phase2Bg'){phase2Bg = new Bitmap(phase2BgImg);}
	
	gfxLoaded++;
	console.log(gfxLoaded+" assets loaded");
	if(gfxLoaded == 8)
	{
		console.log("All Assets Loaded !");
		
		
		//addGameView();
	}
}

function addGameView()
{
	console.log("Placing Scene Elements");
	b1.x = centerX - botWidth/2;
	b1.y = screenHeight - botHeight;

	b2.x = centerX - botWidth/2;
	b2.y = 0;
	
	
	/* Score Text 
	
	score = new Text('0', 'bold 14px Courier New', '#FFFFFF');
	score.maxWidth = 1000;	//fix for Chrome 17
	score.x = 2;
	score.y = 476;

	
	/* Add gfx to stage and Tween Ship 
	
	stage.addChild(bg, b1, b2, score);
	//Tween.get(ship).to({y:425}, 1000).call(startGame);

	console.log("Scene Elements placed !");

}

function shoot()
{
	var b = new Bitmap(bltImg);
	
	b.x = ship.x + 13;
	b.y = ship.y - 20;
	
	bullets.addChild(b);
	
	SoundJS.play('shot');
	stage.update();
}

function addEnemy()
{
	var e = new Bitmap(eImg);
	
	e.x = Math.floor(Math.random() * (320 - 50))
	e.y = -50
	
	enemies.addChild(e);
	stage.update();
}

function moveShip(e)
{
	ship.x = e.stageX - 18.5;
}

function addGameView()
{
	
	/* Score Text 
	
	score = new Text('0', 'bold 14px Courier New', '#FFFFFF');
	score.maxWidth = 1000;	//fix for Chrome 17
	score.x = 2;
	score.y = 476;
}

function Update()
{
/* Move Bullets 
	
	for(var i = 0; i < bullets.children.length; i++)
	{
		bullets.children[i].y -= 10;
		
		/* Remove Offstage Bullets 
		
		if(bullets.children[i].y < - 20)
		{
			bullets.removeChildAt(i);
		}
	}
	
	/* Show Boss *
	
	if(parseInt(score.text) >= 500 && boss == null)
	{
		boss = new Bitmap(bImg);
				
		SoundJS.play('boss');
				
		boss.x = centerX - 90;
		boss.y = -183;
				
		stage.addChild(boss);
		Tween.get(boss).to({y:40}, 2000)
	}
	
	/* Move Enemies *
	
	for(var j = 0; j < enemies.children.length; j++)
	{
		enemies.children[j].y += 5;
		
		/* Remove Offstage Enemies *
		
		if(enemies.children[j].y > 480 + 50)
		{
			enemies.removeChildAt(j);
		}
		
		for(var k = 0; k < bullets.children.length; k++)
		{
			/* Bullet - Enemy Collision *
	
			if(bullets.children[k].x >= enemies.children[j].x && bullets.children[k].x + 11 < enemies.children[j].x + 49 && bullets.children[k].y < enemies.children[j].y + 40)
			{
				bullets.removeChildAt(k);
				enemies.removeChildAt(j);
				stage.update();
				SoundJS.play('explo');
				score.text = parseFloat(score.text + 50);
			}
			
			/* Bullet - Boss Collision *
			
			if(boss != null && bullets.children[k].x >= boss.x && bullets.children[k].x + 11 < boss.x + 183 && bullets.children[k].y < boss.y + 162)
			{
				bullets.removeChildAt(k);
				bossHealth--;
				stage.update();
				SoundJS.play('explo');
				score.text = parseInt(score.text + 50);
			}
		}
		
		/* Ship - Enemy Collision *
		
		if(enemies.hitTest(ship.x, ship.y) || enemies.hitTest(ship.x + 37, ship.y))
		{
			enemies.removeChildAt(j);
			lives.removeChildAt(lives.length);
			ship.y = 480 + 34;
			Tween.get(ship).to({y:425}, 500)
			SoundJS.play('explo');
		}
	}
	
	/* Check for win *
	
	if(boss != null && bossHealth <= 0)
	{
		alert('win');
	}
	
	/* Check for lose *
	
	if(lives.children.length <= 0)
	{
		alert('lose');
	}
}	
	*/